
let id = 0
export default class Dep {
  constructor() {
    this.id = id++
    this.subs = [] //存放watcher的容器

  }
  depend() {
    // 如果当前存在watcher
    if (Dep.target) {
      Dep.target.addDep(this); // 把自身-dep实例存放在watcher里面
    }
  }
  notify() {
    this.subs.forEach(watcher => watcher.update())
  }
  addSub(watcher) {
    // 把watcher加入到自身的subs容器
    this.subs.push(watcher);
  }
}

Dep.target = null //
const targetStack = [] //栈结构来保存watcher

export const pushTarget = (watcher) => {
  targetStack.push(watcher)
  Dep.target = watcher
}

export const popTarget = () => {
  targetStack.pop()
  Dep.target = targetStack[targetStack.length - 1];
}
